package com.cj;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * @author cc
 * @date 2025-05-14 09:05
 **/

public class UserBeanPostProcessor implements BeanPostProcessor {

    // 初始化： 就是执行那些初始化方法 ，， 比如init-method
    // 初始化之前

    /**
     *
     * @param bean 对象本身
     * @param beanName 对象的名字
     * @return  返回bean本身
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("bean = " + bean+"-"+beanName);
        System.out.println("初始化之前");
        if (beanName.equals("user")){
            // user对象
            User user = (User) bean;
            user.setUsername("cj");
            return user;
        }
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }


    // 初始化之后的方法
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("初始化之后");
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
}
